#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
using namespace std;
struct ListNode
{
    int val;
    ListNode* next;
    ListNode(int x = 0) { val = x; }
};

class Solution {
public:
    ListNode* removeZeroSumSublists(ListNode* head) {
        vector<int> vi;
        ListNode* pcur = head;
        while (pcur != nullptr)
        {
            vi.push_back(pcur->val);
            pcur = pcur->next;
        }
        int n = vi.size();
        for (int i = 0; i < n - 1; i++)
        {
            int sum = vi[i];
            for (int j = i + 1; j < n; j++)
            {
                sum += vi[j];
                if (sum == 0)
                {
                    for (int x = i; x <= j; x++)
                        vi[x] = 0;
                    break;
                }
            }
        }
        ListNode* newhead = new ListNode;
        newhead->next = head;
        pcur = newhead;
        for (int i = 0; i < n; i++)
            if (vi[i])
            {
                pcur = pcur->next;
                pcur->val = vi[i];
            }
        pcur->next = nullptr;

        return newhead->next;
    }
};

int main()
{
    //Solution s;
    ListNode* head = new ListNode(-1);
    ListNode ln1(2);
    ListNode ln2(-1);
    ListNode ln3(-2);
    ListNode ln4(1);
    ListNode ln5(1);
    //ListNode ln6(-5);
    head->next = &ln1;
    ln1.next = &ln2;
    ln2.next = &ln3;
    ln3.next = &ln4;
    ln4.next = &ln5;
    ln5.next = nullptr;

    ListNode* pcur = head;
    while (pcur != nullptr)
    {
        cout << pcur->val << ' ';
        pcur = pcur->next;
    }
    Solution s;
    pcur = s.removeZeroSumSublists(head);
    while (pcur != nullptr)
    {
        cout << pcur->val << ' ';
        pcur = pcur->next;
    }
    return 0;
}